/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.gshop.domain.po.UserAddressPo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.pbpm.service.UserAddressService;

@RestController
@RequestMapping("address")
public class UserAddressController extends GenericController {
	private static final long serialVersionUID = -5236966819218679L;
	public static final String CACHE_KEY = "userAddress";
	@Autowired
	private UserAddressService userAddressService;
	
	/**
	 * 根据条件获取UserAddressPo对象，并返回查询的数据总数
	 * 
	 * @param userAddress
	 * @Author 迷蝶
	 * @return ResultPageTo<UserAddressPo>
	 */
	@RequestMapping("list")
    public ResultPageTo<UserAddressPo> getUserAddresss(@RequestBody UserAddressPo userAddress) {
        return userAddressService.getUserAddresss(userAddress);
    }
	
	/**
	 * 根据ID获取UserAddressPo对象，并放入redis缓存
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return UserAddressPo
	 */
	@RequestMapping("{id:^[0-9]*$}")
	@Cacheable(value=CACHE_KEY, key="'userAddress_'+#id")
	public UserAddressPo getUserAddress(@PathVariable("id") int id) {
		return userAddressService.getUserAddress(id);
	}
	
	/**
	 * 保存UserAddressPo对象，并清除此ID的缓存。<br>
	 * 如果ID存在，则更新对象；ID不存在则插入对象。
	 * 
	 * @param userAddress
	 * @Author 迷蝶
	 * @return Integer 如果成功，则为新增或修改对象的ID；反之则为0
	 */
	@RequestMapping("save")
	@CacheEvict(value=CACHE_KEY, key="'userAddress_'+#userAddress.id")
	public Integer saveUserAddress(@RequestBody UserAddressPo userAddress) {
		return userAddressService.saveUserAddress(userAddress) > 0 ? userAddress.getId() : 0;
	}
	
	/**
	 * 根据ID删除UserAddressPo对象，并清除此ID的缓存。<br>
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return UserAddressPo
	 */
	@RequestMapping("del/{id:^[0-9]*$}")
	@CacheEvict(value=CACHE_KEY, key="'userAddress_'+#id")
	public Integer delUserAddress(@PathVariable("id") int id) {
		return userAddressService.delUserAddress(id);
	}
	
	/**
	 * 根据用户ID获取默认地址
	 * 
	 * @param userId	用户ID，只能是数字
	 * @Author 迷蝶
	 * @return UserAddressPo
	 */
	@RequestMapping("default/{userId:^[0-9]*$}")
	public UserAddressPo getDefaultAddr(@PathVariable("userId") int userId) {
		return userAddressService.getDefaultAddr(userId);
	}
}